package com.xuedongzhilian.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xuedongzhilian.dto.ExamPaperDTO;
import com.xuedongzhilian.entity.ExamPaper;
import com.xuedongzhilian.mapper.ExamPaperMapper;
import com.xuedongzhilian.service.IExamPaperService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 试卷练习服务实现类
 * </p>
 *
 * @author 学动智联团队
 * @since 2025-10-14
 */
@Service
public class ExamPaperServiceImpl implements IExamPaperService {

    @Resource
    private ExamPaperMapper examPaperMapper;

    @Override
    public boolean createExamPaper(ExamPaperDTO examPaperDTO) {
        ExamPaper examPaper = BeanUtil.copyProperties(examPaperDTO, ExamPaper.class);
        examPaper.setCreateTime(LocalDateTime.now());
        examPaper.setUpdateTime(LocalDateTime.now());
        return examPaperMapper.insert(examPaper) > 0;
    }

    @Override
    public List<ExamPaperDTO> getExamPapersByUserId(Long userId) {
        QueryWrapper<ExamPaper> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        List<ExamPaper> examPapers = examPaperMapper.selectList(queryWrapper);
        return examPapers.stream()
                .map(examPaper -> BeanUtil.copyProperties(examPaper, ExamPaperDTO.class))
                .collect(Collectors.toList());
    }

    @Override
    public ExamPaperDTO getExamPaperDetail(Long paperId, Long userId) {
        QueryWrapper<ExamPaper> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", paperId).eq("user_id", userId);
        ExamPaper examPaper = examPaperMapper.selectOne(queryWrapper);
        if (examPaper == null) {
            return null;
        }
        return BeanUtil.copyProperties(examPaper, ExamPaperDTO.class);
    }

    @Override
    public boolean updateExamPaper(ExamPaperDTO examPaperDTO) {
        ExamPaper examPaper = BeanUtil.copyProperties(examPaperDTO, ExamPaper.class);
        examPaper.setUpdateTime(LocalDateTime.now());
        return examPaperMapper.updateById(examPaper) > 0;
    }

    @Override
    public boolean deleteExamPaper(Long paperId, Long userId) {
        QueryWrapper<ExamPaper> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", paperId).eq("user_id", userId);
        return examPaperMapper.delete(queryWrapper) > 0;
    }

    @Override
    public boolean startExamPaper(Long paperId, Long userId) {
        ExamPaper examPaper = new ExamPaper();
        examPaper.setId(paperId);
        examPaper.setStatus(2); // 2表示进行中
        examPaper.setStartTime(LocalDateTime.now());
        examPaper.setUpdateTime(LocalDateTime.now());
        QueryWrapper<ExamPaper> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", paperId).eq("user_id", userId);
        return examPaperMapper.update(examPaper, queryWrapper) > 0;
    }

    @Override
    public boolean completeExamPaper(Long paperId, Long userId, Integer actualScore, Integer usedTime) {
        ExamPaper examPaper = new ExamPaper();
        examPaper.setId(paperId);
        examPaper.setStatus(3); // 3表示已完成
        examPaper.setActualScore(actualScore);
        examPaper.setUsedTime(usedTime);
        examPaper.setFinishTime(LocalDateTime.now());
        examPaper.setUpdateTime(LocalDateTime.now());
        QueryWrapper<ExamPaper> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", paperId).eq("user_id", userId);
        return examPaperMapper.update(examPaper, queryWrapper) > 0;
    }
}